package m.member.store.action;

import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.common.model.util.FromQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueCollectResult;
import m.html.VueListResult;
import m.html.VueTabResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.list.*;
import m.html.tab.TabItem;
import m.member.order.action.OrderInfoAction;
import m.member.order.model.MemberRecharge;
import m.member.order.model.OrderInfo;
import m.member.order.model.RechargeOffset;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import manage.action.ManageAction;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;

import java.util.Date;

@ActionMeta(name = "member/store/rechargeCollect")
public class RechargeCollectAction extends CollectAction {
    public VueResult toRechargeTab(){
        String menu=getParamsValue("menu","month");
        String title="month".equals(menu)?"充值月度汇总":"充值年度汇总";
        VueTabResult tab=new VueTabResult(title);
        tab.setSubTitle("{{tabs.names[indexKey]}}");
        Date date=new Date();
        if("month".equals(menu)){
            for(int i=0;i<=5;i++){
                Date d= DateUtil.getAddMonth(date,-i);
                String type=DateUtil.format(d,"yyyy-MM");
                String name=DateUtil.format(d,"yyyy年MM月");
                tab.addItems(new TabItem(menu+type,name,actionUrl("toRechargeCollect"),
                        new JSONMessage().push("params",new JSONMessage().push("menu",menu).push("type",type))));
            }
        }else{
            for(int i=0;i<=5;i++){
                Date d=DateUtil.getAddMonth(date,-i*12);
                String type=DateUtil.format(d,"yyyy");
                String name=DateUtil.format(d,"yyyy年");
                tab.addItems(new TabItem(menu+type,name,actionUrl("toRechargeCollect"),
                        new JSONMessage().push("params",new JSONMessage().push("menu",menu).push("type",type))));
            }
        }
        return tab;
    }
    public VueResult toRechargeCollect() throws MException {
        OrgGroupView org=getSessionOrgGroup();
        if(null==org|| ObjectUtil.isEmptyOid(org.getDept()))
            throw new MException(this.getClass(),"无店铺信息");
        String menu=getParamsValue("menu","month");
        String type=getParamsValue("type","");
        String title="month".equals(menu)?"充值月度汇总":"充值年度汇总";
        FromQueryList from=FromQueryList.fromGroup("a", MemberRecharge.class);
        from.addCondition("#{a.status}='Y' and #{a.inputDept.oid}=?",org.getDept().getOid());
        VueCollectResult result=getVueCollectResult(title,from,actionUrl("toRechargeCollect"));
        result.setPageSize(20);
        result.setDefaultSearchNum(3);
        result.setHeight(-1);
        result.hideHead();
        result.setFunction(true,true);
        result.setMergeCellsInfo(0,1);
        if("month".equals(menu)){
            Date date=DateUtil.format(type,"yyyy-MM");
            result.setSubTitle(DateUtil.format(date,"yyyy年MM月"));
            from.addCondition("date_format(#{a.inputDate},'%Y-%m')='"+ type +"'");
            from.setOrder("date_format(#{a.inputDate},'%d') asc,#{a.type} asc");
            result.addCol(collectTextColumn("日期","date_format(#{a.inputDate},'%d') dayNum").setDisItems(getDayItem()));
            result.addSearch(ListSearch.field("日期","dayNum", SearchType.STR_EQ).setDisItems(getDayItem()));
        }else{
            Date date=DateUtil.format(type,"yyyy");
            result.setSubTitle(DateUtil.format(date,"yyyy年"));
            from.addCondition("date_format(#{a.inputDate},'%Y')='"+ type +"'");
            from.setOrder("date_format(#{a.inputDate},'%m') asc,#{a.type} asc");
            result.addCol(collectTextColumn("月份","date_format(#{a.inputDate},'%m') dayNum").setDisItems(getMonthItem()));
            result.addSearch(ListSearch.field("月份","dayNum", SearchType.STR_EQ).setDisItems(getMonthItem()));
        }
        result.addCol(collectTextColumn("服务员", "#{a.serveAdmin.oid} serveAdmin", 100).setSubtotal(true).setDictType(AdminLogin.class));
        result.addCol(collectTextColumn("付款类型", "#{a.type} payType", 100).setSubtotal(true).setDictType("m.member.order.model.MemberRecharge!type"));
        result.addCol(collectIntegerColumn("充值次数","count(#{a.oid}) rechargeNum"));
        result.addCol(collectNumberColumn("实付金额","sum(#{a.payAmount}) payAmount"));
        result.addCol(collectNumberColumn("赠送金额","sum(#{a.giveAmount}) giveAmount"));
        result.addCol(collectIntegerColumn("赠送积分","sum(#{a.givePoint}) givePoint"));
        result.addSearch(ListSearch.field("服务员","serveAdmin", SearchType.STR_EQ).setDictType("manage.model.AdminLogin"));
        result.addSearch(ListSearch.field("付款类型","payType", SearchType.STR_EQ).setDictType("m.member.order.model.MemberRecharge!type"));
        return result;
    }
    /*
     * 店铺结算
     */
    public VueResult rechargeOffset() throws Exception {
        OrgGroupView org=getSessionOrgGroup();
        if(null==org|| ObjectUtil.isEmptyOid(org.getDept()))
            throw new MException(this.getClass(),"无店铺信息");
        String menu=getParamsValue("menu","in");
        VueListResult result;
        if("in".equals(menu)){
            result=getVueListResult("结算店铺明细", RechargeOffset.class,actionUrl("rechargeOffset"));
            result.setSubTitle("需给[订单店铺]结算服务金额");
            result.addCol(toIndexColumn());
            result.addCondition(QueryCondition.eq("inDept.oid",org.getDept().getOid()));
            result.addCol(toTextColumn("充值店铺", "inDept.name", 100));
            result.addCol(toTextColumn("订单店铺", "outDept.oid", 100, true).setDictType(OrgGroupView.class,"C").setDictTree(true));
        }else{
            result=getVueListResult("店铺服务明细", RechargeOffset.class,actionUrl("rechargeOffset"));
            result.setSubTitle("已给[充值店铺]提供服务明细");
            result.addCol(toIndexColumn());
            result.addCondition(QueryCondition.eq("outDept.oid",org.getDept().getOid()));
            result.addCol(toTextColumn("订单店铺", "outDept.name", 100));
            result.addCol(toTextColumn("充值店铺", "inDept.oid", 100, true).setDictType(OrgGroupView.class,"C").setDictTree(true));
        }
        result.setFunction(true,true);
        result.setHeight(-1);
        result.addCol(toDateColumn("时间", "inputDate", 100, true));
        result.addCol(toNumberColumn("付款金额", "payAmount", 100, true));
        result.addCol(toNumberColumn("赠送金额", "giveAmount", 100, true));
        result.addCol(toSpaceColumn("查看", 100,
            ListButton.modal("订单", ButtonType.LINK, actionUrl(OrderInfoAction.class,"toOrderInfo")+"?edit=false", 1100)
                .addParam("model.oid", "order.oid")
        ).setFixed(Align.RIGHT));
        result.addCondition(QueryCondition.eq("status","N"));
        return result;
    }
}
